<?php


namespace app\admin\model;


class TombareaModel extends BaseModel
{
    protected $name='tombarea';



    public function doDataSave($parem){

        $tm = $parem['tm'];
        $cate_id = $tm['cate_id'];
        $mId = '';
        $this->startTrans();
        try {
            if (empty($tm['name'])) {
                $this->error = "请填写名称！";
                $this->rollback();
                return false;
            }
            if (!isset($parem['row'])) {
                $this->error = "请生成排号，并写入墓位编号！";
                $this->rollback();
                return false;
            }
            unset($tm['parentid']);unset($tm['id']);
            $tm['add_user'] = cmf_get_current_admin_id();
            $tm['add_time'] = time();
            $tombarea_id = $this->insertGetId($tm);
            foreach ($parem['row'] as $k_r => &$row_value) {
                if (empty($row_value['num'])) {
                    $this->error = "请输入墓位排号！";
                    $this->rollback();
                    return false;
                }
                if (empty($row_value['column'])) {
                    $this->error = "请输入‘" . $row_value['num'] . "排’位数！";
                    $this->rollback();
                    return false;
                }
                if (!isset($row_value['col'])) {
                    $this->error = "请生成‘" . $row_value['num'] . "排’！";
                    $this->rollback();
                    return false;
                }
                $row_arr['num'] = $row_value['num'];
                $row_arr['column'] = $row_value['column'];
                $row_arr['tombarea_id'] = $tombarea_id;
                $rowid = TombareaRowModel::insertGetId($row_arr);
                foreach ($row_value['col'] as $k_c => &$col_value) {
                    if (empty($col_value['num'])) {
                        $this->error = "请输入墓位编号！";
                        $this->rollback();
                        return false;
                    }
                    $col_info = TombareaRowColModel::where(["num" => $col_value['num']])->find();
                    if (!empty($col_info)) {
                        $this->error = $col_value['num'] . '墓位编号重复！';
                        $this->rollback();
                        return false;
                    } else {
                        $col_value['cate_id'] = $cate_id;
                        $col_value['rowid'] = $rowid;
                        $col_value['column_num'] = $k_c + 1;
                        $col_value['col_status'] = $cate_id == 3 ? 0 : ($cate_id == 4 ? 0 : 4);
                        $col_value['reacher'] = '';
                        $col_value['rows_num'] = $row_value['num'];
                        $col_value['tombarea_id'] = $tombarea_id;
                        $colId = TombareaRowColModel::insertGetId($col_value);
                        $mId = !empty($mId) ? $mId . ',' . $colId : $colId;
                    }
                }
            }
            $this->commit();
            return ['tombid'=>$tombarea_id,'col'=>$mId];
        }catch(\Exception $e){
            $this->error = '添加墓位失败！';
            $this->rollback();
            return false;
        }
    }

    /**
     *  修改墓位状态 是否和墓穴相同数量
     * @param $mwId
     */
    public function tomDelete($id){
        $this->startTrans();
        // 物理删除
        try {

            $num = $this->where(array('id' => $id))->delete();

            if ($num !=0) {
                // 删除dj_tombarea_row_col和dj_tombarea_row表的该墓区下的数据
                $col_res =  TombareaRowColModel::where(['tombarea_id' => $id])->delete();
                $col_res =  TombareaRowModel::where(array('tombarea_id' => $id))->delete();
                $col_res =  TombareaInformationProductModel::where(array('tombar_id' => $id))->delete();
            }
            if($num != 0){
                $this->commit();
                return true;
            } else {
                $this->error = "删除失败";
                $this->rollback();
                return  false;
            }

        }catch (\Exception $e) {
            $this->error = "删除墓区失败!";
            $this->rollback();
            return  false;
        }
    }



}